<template>
  <view class="newFriends">
    <topTitle title="新的好友">
      <template #right>
        <i @tap="goTo('/pages/create/addFriend')"></i>
      </template>
    </topTitle>
    <view class="com">
      <view class="newFriendsList" v-if="friendApplyList?.length">
        <view
          v-for="(item, idx) in friendApplyList"
          :key="idx"
          @tap="goDetail({ ...item, ...item.apply_user, type: 1 })"
        >
          <view>
            <up-avatar
              mode="aspectFill"
              :src="item.apply_user.nick_name.picture"
              size="56"
            ></up-avatar>
            <view>
              <view>{{ item.apply_user.nick_name }}</view>
              <view>{{ item.describe }}</view>
            </view>
          </view>
          <view @tap.stop="friendApplyApproveFn(item)">同意</view>
        </view>
      </view>
      <up-empty
        icon="/static/image//noData/1.png"
        text="暫無好友申請"
        marginTop="120"
        v-else
      ></up-empty>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, watch, computed } from "vue";
const instance = getCurrentInstance()?.appContext.config.globalProperties;
import { onShow } from "@dcloudio/uni-app";
import { useStore } from "@/stores";
import { FriendApply, FriendApplyApprove } from "@/api/ImApi";
const store = useStore();
const latestData = computed(() => store.latestData);
watch(latestData, (newValue:any) => {
  // 通知消息
  if (newValue.Grade == 2) {
    FriendApplyFn();
  }
});
// 申請列表
let friendApplyList = ref<Array<any>>();
const FriendApplyFn = async () => {
  if (uni.getStorageSync("FriendApply")) {
    friendApplyList.value = uni.getStorageSync("FriendApply");
  }
  friendApplyList.value = await FriendApply();
};
// 查看詳情
const goDetail = async (item: any) => {
  instance?.goTo(
    "/pages/create/searchAdd?searchValue=" +
      encodeURIComponent(JSON.stringify(item))
  );
};
// 好友申請通過;
const friendApplyApproveFn = async (item: any) => {
  await FriendApplyApprove({
    id: item.Id,
    loading: 1,
  });
  FriendApplyFn();
  uni.showToast({ title: "好友申請通過", icon: "none" });
};
onShow(() => {
  FriendApplyFn();
});
</script>
<style scoped lang="scss">
@import "../../static/scss/addressBook.scss";
</style>
